package ControlLayer;
import ModelLayer.*;
import DBLayer.*;
import java.util.ArrayList;

public class ProductCtr
{
	SupplierCustomerCtr supplierCtr = new SupplierCustomerCtr();
	public ProductCtr()
	{
		
	}

	public ArrayList<Product> findAllProducts()
	{
		IFDBProduct dbProduct = new DBProduct();
		ArrayList<Product> allProd = new ArrayList<Product>();
		allProd = dbProduct.getAllProducts(false);
		return allProd;
	}

	public Product findByDescription(String description)
	{
		IFDBProduct dbProduct = new DBProduct();
		return dbProduct.searchProductDescription(description, true);
	}

	public Product findByID(int id)
	{
		IFDBProduct dbProduct = new DBProduct();
		return dbProduct.findProduct(id, true);
	}

	public int updateProduct(String name, String description, String status, String storage, double price, Supplier supplier, String clothes_size, String clothes_colour, String equipment_type, String equipment_desc, String gunRep_material, String gunRep_caliber)
	{
		IFDBProduct dbProduct = new DBProduct();
		Product product = new Product();
		product.setName(name);
		product.setDescription(description);
		product.setStatus(status);
		product.setStorage(storage);
		product.setPrice(price);
		product.setSupplier(supplier);
		product.setClothes_size(clothes_size);
		product.setClothes_colour(clothes_colour);
		product.setEquipment_type(equipment_type);
		product.setEquipment_desc(equipment_desc);
		product.setGunRep_material(gunRep_material);
		product.setGunRep_caliber(gunRep_caliber);
		return dbProduct.updateProduct(product);
	}

	public void insertNew(String name, String description, String status, String storage, double price, Supplier supplier, String clothes_size, String clothes_colour, String equipment_type, String equipment_desc, String gunRep_material, String gunRep_caliber)
	{
		Product prodObj = new Product();
		prodObj.setName(name);
		prodObj.setDescription(description);
		prodObj.setStatus(status);
		prodObj.setStorage(storage);
		prodObj.setPrice(price);
		prodObj.setSupplier(supplier);
		prodObj.setClothes_size(clothes_size);
		prodObj.setClothes_colour(clothes_colour);
		prodObj.setEquipment_type(equipment_type);
		prodObj.setEquipment_desc(equipment_desc);
		prodObj.setGunRep_material(gunRep_material);
		prodObj.setGunRep_caliber(gunRep_caliber);
		
		try
		{
			DbConnection.startTransaction();
			DBProduct dbProd = new DBProduct();
			dbProd.insertProduct(prodObj);
			DbConnection.commitTransaction();
		}
		catch(Exception e)
		{
			DbConnection.rollbackTransaction();
		}
	}
}
